package cn.liumouren.jzxd.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.Query;
import cn.liumouren.jzxd.dao.FavoriteDao;
import cn.liumouren.jzxd.entity.Favorite;
import cn.liumouren.jzxd.entity.Goods;
import cn.liumouren.jzxd.service.FavoriteService;
import cn.liumouren.jzxd.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;


@Service("favoriteService")
public class FavoriteServiceImpl extends ServiceImpl<FavoriteDao, Favorite> implements FavoriteService {

    @Autowired
    private GoodsService goodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Favorite> page = this.page(
                new Query<Favorite>().getPage(params),
                new QueryWrapper<Favorite>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Favorite> findByUserId(String userId) {
        List<Favorite> favorites = super.list(new QueryWrapper<Favorite>().lambda()
                .eq(Favorite::getUserId, userId));

        return favorites;
    }

    @Override
    public void deleteByObjectIdAndUserId(String objectId, String userId) {
        baseMapper.delete(new LambdaQueryWrapper<Favorite>().eq(Favorite::getUserId, userId)
                .and(e -> e.eq(Favorite::getSceneId, objectId)
                        .or()
                        .eq(Favorite::getGoodsId, objectId)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Favorite entity) {
        entity.setId(IdWorker.getIdStr());
        entity.setAddTime(new Date());
        // 如果是商品, 商品收藏数加 1
        if (!StrUtil.isEmpty(entity.getGoodsId())) {
            goodsService.update(new UpdateWrapper<Goods>()
                    .setSql(" set follower = follower + 1 ")
                    .eq("id", entity.getGoodsId()));
        }
        return SqlHelper.retBool(baseMapper.insert(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Favorite entity) {
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return false;
        }
        List<Favorite> favoriteList = super.listByIds(idList);
        if (!CollUtil.isEmpty(favoriteList)) {
            Set<String> goodsIds = favoriteList.stream().map(Favorite::getGoodsId).collect(Collectors.toSet());
            if (!CollUtil.isEmpty(goodsIds)) {
                goodsService.update(new UpdateWrapper<Goods>()
                        .setSql(" set follower = follower - 1 ")
                        .in("id", goodsIds));
            }
        }
        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

}